export type textPositionType = {
  x: number // 文字定位
  y: number // 文字定位
  width: number // 文字宽度
  height: number // 文字高度
  transform: number // 文字旋转角度
  color: string // 文本颜色
  text: string // 点击的文字
}

export type clickPositionType = {
  x: number // 文字定位
  y: number // 文字定位
  width: number // 文字宽度
  height: number // 文字高度
  left: number // 距离左侧位置
  top: number // 距离顶部位置
}

// 检测两个矩形是否重叠的函数
export const isOverlapping = (currentPoint: textPositionType, targetPoint: textPositionType) => {
  return !(currentPoint.x + currentPoint.width <= targetPoint.x || targetPoint.x + targetPoint.width <= currentPoint.x || currentPoint.y + currentPoint.height <= targetPoint.y || targetPoint.y + targetPoint.height <= currentPoint.y)
}

// 检测两个点击的锚点是否重叠
export const isOverlapPosition = (currentPoint: clickPositionType, targetPoint: clickPositionType) => {
  return currentPoint.x >= targetPoint.left && currentPoint.x <= targetPoint.left + targetPoint.width && currentPoint.y >= targetPoint.top && currentPoint.y <= targetPoint.top + targetPoint.height
}

// 检测点击点与需点击文字是否重叠
export const isOverlap = (currentPoint: clickPositionType, targetPoint: textPositionType) => {
  return currentPoint.x >= targetPoint.x && currentPoint.x <= targetPoint.x + targetPoint.width && currentPoint.y >= targetPoint.y && currentPoint.y <= targetPoint.y + targetPoint.height
}

// 生成随机不重叠位置的文字函数
export const generateRandomPosition = (existingPositions: textPositionType[], parentWidth: number, parentHeight: number, elementWidth: number, elementHeight: number, angleRange: number) => {
  let position
  let overlap
  do {
    overlap = false
    const left = Math.floor(Math.random() * (parentWidth - elementWidth))
    const top = Math.floor(Math.random() * (parentHeight - elementHeight))
    let leftVal = 0
    let topVal = 0
    if (left < elementWidth) {
      leftVal = elementWidth
    } else if (left >= elementWidth && left < parentWidth - elementWidth * 2) {
      leftVal = left
    } else {
      leftVal = left - elementWidth * 2
    }
    if (top < elementHeight * 2) {
      topVal = elementHeight * 2
    } else if (top >= elementHeight * 2 && top < parentHeight - elementHeight * 2) {
      topVal = top
    } else {
      topVal = top - elementHeight * 2
    }
    position = {
      x: leftVal,
      y: topVal,
      width: elementWidth,
      height: elementHeight,
      transform: Math.floor(Math.random() * (angleRange + angleRange + 1)) - angleRange,
      color: '',
      text: ''
    }

    // 检查新位置是否与现有位置重叠
    for (const existing of existingPositions) {
      if (isOverlapping(position, existing)) {
        overlap = true
        break
      }
    }
  } while (overlap)
  return position
}

// 生成指定个数的随机数，用来获取文本，生成的随机数即每个文本的索引
export const getRandomUniqueIndexes = (max: number, count: number) => {
  const indexes: number[] = []
  while (indexes.length < count) {
    const randomIndex = Math.floor(Math.random() * max)
    if (!indexes.includes(randomIndex)) {
      indexes.push(randomIndex)
    }
  }
  return indexes
}
